2 using System.Collections.Generic;
5 using System.Threading;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
10 namespace SuperPolarity
15 public static Color BlueColor;
16 public static Color RedColor;
18 ParticleEngine particleEngine;
20 protected bool Shooting;
21 protected int ShotCooldown;
23 protected float CurrentImmortalTime;
24 protected float MaxImmortalTime;
25 protected bool Flashing;
27 public MainShip(SuperPolarity newGame) : base(newGame) {}
31 particleEngine = null;
34 public override void Initialize(Texture2D texture, Vector2 position)
36 base.Initialize(texture, position);
38 MainShip.BlueColor = new Color(230, 244, 249);
39 MainShip.RedColor = new Color(255, 234, 241);
42 SetPolarity(Polarity.Positive);
45 MaxImmortalTime = 1500;
56 void InitParticleEngine()
58 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
63 InputController.Bind("moveX", HandleHorizontalMovement);
64 InputController.Bind("moveY", HandleVerticalMovement);
65 InputController.Bind("changePolarity", HandleChangePolarity);
66 InputController.Bind("shoot", HandleShot);
69 protected void HandleShot(float value)
71 var bullet = ActorFactory.CreateBullet(Position, Angle);
77 Timer t = new Timer(new TimerCallback(UnlockShot));
78 t.Change(ShotCooldown, Timeout.Infinite);
81 protected void UnlockShot(object state)
83 InputController.Unlock("shoot");
86 protected void HandleChangePolarity(float value)
91 public void HandleHorizontalMovement(float value)
93 Acceleration.X = value * AccelerationRate;
95 if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
100 if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
106 public void HandleVerticalMovement(float value)
108 Acceleration.Y = value * AccelerationRate;
111 public override void SwitchPolarity()
113 base.SwitchPolarity();
114 SwitchParticleEngine(CurrentPolarity);
115 game.Player.ResetMultiplier();
118 public override void SetPolarity(Polarity newPolarity)
120 base.SetPolarity(newPolarity);
121 SwitchParticleEngine(newPolarity);
124 protected void SwitchParticleEngine(Polarity polarity)
126 if (polarity == Polarity.Positive)
128 particleEngine.Color = MainShip.RedColor;
130 else if (polarity == Polarity.Negative)
132 particleEngine.Color = MainShip.BlueColor;
136 particleEngine.Color = Color.Gray;
140 public override void Update(GameTime gameTime)
142 base.Update(gameTime);
143 particleEngine.EmitterLocation = Position;
144 particleEngine.Update();
146 UpdateImmortality(gameTime);
150 public void UpdateImmortality(GameTime gameTime)
154 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
158 Color = new Color(255, 255, 255, 128);
165 Flashing = !Flashing;
167 if (CurrentImmortalTime > MaxImmortalTime)
175 public override void Move(GameTime gameTime)
181 if (Velocity.X > ActVelocity)
183 Velocity.X = ActVelocity;
186 if (Velocity.X < -ActVelocity)
188 Velocity.X = -ActVelocity;
191 if (Velocity.Y > ActVelocity)
193 Velocity.Y = ActVelocity;
196 if (Velocity.Y < -ActVelocity)
198 Velocity.Y = -ActVelocity;
203 public override void Magnetize(Ship ship, float distance, float angle)
207 protected void ConstrainToEdges()
218 if (Position.X > game.GraphicsDevice.Viewport.Width)
220 Position.X = game.GraphicsDevice.Viewport.Width;
236 if (Position.Y > game.GraphicsDevice.Viewport.Height)
238 Position.Y = game.GraphicsDevice.Viewport.Height;
247 public override void Draw(SpriteBatch spriteBatch)
249 particleEngine.Draw(spriteBatch);
250 base.Draw(spriteBatch);
253 public override void Collide(Actor other, Rectangle collision)
255 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
262 protected override void Die()
264 game.Player.Lives = game.Player.Lives - 1;
265 game.Player.ResetMultiplier();
266 if (game.Player.Lives < 0)
272 CurrentImmortalTime = 0;